package com.dayang.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dayang.model.Userif;
import com.dayang.service.UserService;
import com.shiro.utils.CSiUtil;

@Controller
@RequestMapping("/login")
public class LoginContorllor {
	Logger log =LogManager.getLogger(LoginContorllor.class);
	
	@Resource
	private UserService userService;
	
	
	@RequestMapping(value = "/login")
    public String showLoginForm(HttpServletRequest req, Model model) {
        String exceptionClassName = (String)req.getAttribute("shiroLoginFailure");
        String error = null;
        if(UnknownAccountException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
            error = "用户名/密码错误";
        } else if(exceptionClassName != null) {
            error = "其他错误：" + exceptionClassName;
        }
        model.addAttribute("error", error);
        System.out.println("获取的token是："+CSiUtil.getCurrentAsToken());
        return "index";
    }

	
/*	@RequestMapping(value = "login/loginin.do", method = RequestMethod.POST)  
	public String formTag(HttpSession session, HttpServletRequest request) {  
	    String url = request.getHeader("Referer");//获取上个页面的url  
	    String username = request.getParameter("username");
	    String password = request.getParameter("password");
	    if("01".equals(username) && "1".equals(password)){
	    	  System.out.println(username);
	  	    session.setAttribute("redirectUrl", url);//把url放到session  
	  	    session.setAttribute("username", username);//把url放到session  
	  	    return "index"; 	
	    }else{
	    	  System.out.println(username);
	  	    return "login"; 
	    	
	    }
	   
	}  */
	/* @RequestMapping(value = "/login", method = RequestMethod.POST)  
	    public ModelAndView login() {  
	        return new ModelAndView("/loginin.do");  
	    }  
	 
	 @RequestMapping(value = "/dologin")  
	 public String doLogin(HttpServletRequest request, Model model) {  
	     String msg = "";  
	     String url = request.getHeader("Referer");//获取上个页面的url  
		    String username = request.getParameter("username");
		    String password = request.getParameter("password");
		    log.info("获取的用户名是："+username);
	     System.out.println(username);  
	     System.out.println(password);  
	     OAuth2Token token = new OAuth2Token(); 
	     token.setPassword(password.toCharArray());
	     token.setUsername(username);
	     token.setRememberMe(true);  
	     Subject subject = SecurityUtils.getSubject();  
	     try {  
	         subject.login(token);  
	         if (subject.isAuthenticated()) {  
	             return "index";  
	         } else {  
	             return "login";  
	         }  
	     } catch (IncorrectCredentialsException e) {  
	         msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     } catch (ExcessiveAttemptsException e) {  
	         msg = "登录失败次数过多";  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     } catch (LockedAccountException e) {  
	         msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     } catch (DisabledAccountException e) {  
	         msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     } catch (ExpiredCredentialsException e) {  
	         msg = "帐号已过期. the account for username " + token.getPrincipal() + "  was expired.";  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     } catch (UnknownAccountException e) {  
	         msg = "帐号不存在. There is no user with username of " + token.getPrincipal();  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     } catch (UnauthorizedException e) {  
	         msg = "您没有得到相应的授权！" + e.getMessage();  
	         model.addAttribute("message", msg);  
	         System.out.println(msg);  
	     }  
	     return "login";  
	 }   */
	/*@RequestMapping(value = "/loginin.do", method = RequestMethod.POST)  
	public ModelAndView  formTag(HttpSession session, HttpServletRequest request) {  
	    String url = request.getHeader("Referer");//获取上个页面的url  
	    String username = request.getParameter("username");
	    String password = request.getParameter("password");
	    log.info("获取的用户名是："+username);
	   
	    //List<Userif> list2 = userService.findAll();
	    List<Userif> list = userService.findByUsername(username);
	    try {
	    	 Userif  u = new Userif();
	 	    u.setId("3333312");
	 	    u.setUsername("100");
	 	    u.setUserpassword("100");
	 	    u.setRole(2);
	 	    userService.mergeItem(u);
		} catch (Exception e) {
			e.printStackTrace();
			 log.info("保存用户名是："+e.getMessage());
		}
	    if(list!=null && list.size()>0){
	    	Userif user = list.get(0);
	    	if(user.getUserpassword().equals(password)){
	    		 session.setAttribute("redirectUrl", url);//把url放到session  
	 	  	    session.setAttribute("username", username);//把url放到session  
	 	  	  return new ModelAndView(new RedirectView("../index.jsp"));  
	    	}else{
	    		
	    		return new ModelAndView(new RedirectView("../login.jsp")); 
	    	}
	    	
	    }else{
	    	
	    	return new ModelAndView(new RedirectView("../login.jsp")); 
	    }
	    if("01".equals(username) && "1".equals(password)){
	    	
	    	  System.out.println(username);
	  	    session.setAttribute("redirectUrl", url);//把url放到session  
	  	    session.setAttribute("username", username);//把url放到session  
	  	  return new ModelAndView(new RedirectView("../index.jsp"));  
	    }else{
	    	  System.out.println(username);
	    	  return new ModelAndView(new RedirectView("../login.jsp"));  
	    	
	    }
	   
	} */
	

}
